package com.dou.controller;

import com.dou.mapper.EmployeeDao;
import com.dou.mapper.LoginDao;
import com.dou.pojo.login;
import com.dou.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA 2021.1 x64
 *
 * @Author: 窦义超
 * @Date: 2021/07/2021/7/19/13:03
 * @Description:
 */
@Controller
public class loginController {

    @Autowired
    LoginDao loginDao;

        @RequestMapping("/user/login")
       // @ResponseBody 不跳视图
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session){
            List<login> allpwd = loginDao.getAllpwd();
            int count=0;
            for (int i = 0; i < allpwd.size(); i++) {
                if (username.equals(allpwd.get(i).getUsername())&&allpwd.get(i).getPassword().equals(password)) {
                    count++;
                    Tools.name = allpwd.get(i).getUsername();
                    Tools.user_id=allpwd.get(i).getUserid();
                    System.out.println(Tools.name);
                    System.out.println(Tools.user_id);
                }
            }
            if (count==1) {
            session.setAttribute("loginUser",username);
            return "redirect:/main.html";
        }else{
            model.addAttribute("msg","用户名或密码错误");
               return "index";
        }
        }
        @RequestMapping("/user/logout")
        public String loginOut(HttpSession session){
session.invalidate();
        return "redirect:/index.html";
        }

        @RequestMapping("/updatepwd")
    public String getpwd(Model model){
            List<login> allpwd = loginDao.getAllpwd();
            model.addAttribute("all",allpwd);
            model.addAttribute("user",Tools.name);
            return "updatePassword";
        }
        @GetMapping("toUpdate")
    public String toupdate(@RequestParam  (value = "userid") int userid,Model model){
            login pwdById = loginDao.getPwdById(userid);
            model.addAttribute("pwd",pwdById);
            return "updatepwd";
        }
        @PostMapping("toUpdate")
    public String tooupdate(login login){
            loginDao.updateById(login);
            return "redirect:/updatepwd";
    }
    @RequestMapping("/toZhuce")
    public String toZhuce1(Model model){
            model.addAttribute("user",Tools.name);
            return "login/zhuce";
    }
    @PostMapping("/zhuce")
    public String zhuce(login l1){
            loginDao.insertLogin(l1);
            return "redirect:/main.html";
    }
}
